#include "total.h"
#include "ui_total.h"
#include "mainwindow.h"

Total::Total(MainWindow *mainWindow, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Total),
    mainwindow(mainWindow)
{
    ui->setupUi(this);
    Total_init();
    Serial_init();
}

void Total::Total_init()
{
    tcpsocket = new QTcpSocket(this);
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(pushButton_pressed()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(pushButton_2_pressed()));
    connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(pushButton_3_clicked()));
    connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(pushButton_4_clicked()));
    connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(reciveMessages()));
    connect(tcpsocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(mstateChanged(QAbstractSocket::SocketState)));
    ui->lineEdit->setEnabled(false);
    ui->lineEdit_2->setEnabled(false);
    ui->lineEdit->setText(QString("%1").arg(countNum(mainwindow->Num)));
    ui->lineEdit_2->setText(QString("%1").arg(countPrice(mainwindow->Num,mainwindow->Price)));
    message = new Message(this,nullptr);
    beep = new Beep();
}

void Total::Serial_init()
{
    serialport = new QSerialPort(this);
    /*设置使用的串口*/
    serialport->setPortName("/dev/ttymxc2");
    /*设置波特率*/
    serialport->setBaudRate(115200);
    serialport->setDataBits(QSerialPort::Data8);
    serialport->setParity(QSerialPort::NoParity);
    serialport->setStopBits(QSerialPort::OneStop);
    serialport->setFlowControl(QSerialPort::NoFlowControl);
}

Total::~Total()
{
    delete ui;
}

void Total::pushButton_pressed()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    this->hide();
    mainwindow->show();
}

void Total::pushButton_2_pressed()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    sendData();
    tcpsendData();
    message->show();
}

void Total::reciveMessages()
{
    ui->label_4->setText("服务端：" + tcpsocket->readAll());
}

void Total::mstateChanged(QAbstractSocket::SocketState state)
{
    switch(state)
    {
        case QAbstractSocket::UnconnectedState:
            ui->label_4->setText("与服务端断开连接");
            break;
        case QAbstractSocket::ConnectedState:
            ui->label_4->setText("已连接服务端");
            break;
        default:break;
    }
}

int Total::countNum(int num[])
{
    int sum = 0;
    for(int i = 1;i < 7;i++)
    {
        sum += num[i];
    }

    return sum;
}

int Total::countPrice(int num[],int price[])
{
    int sum_price = 0;
    for(int i = 1;i < 7;i++)
    {
        sum_price += num[i] * price[i];
    }

    return sum_price;
}

void Total::sendData()
{
    if(serialport->open(QIODevice::ReadWrite))
    {
        QByteArray data = QString("@%1 %2 %3 %4 %5 %6\r\n").arg(mainwindow->Num[1])
                                                           .arg(mainwindow->Num[2])
                                                           .arg(mainwindow->Num[3])
                                                           .arg(mainwindow->Num[4])
                                                           .arg(mainwindow->Num[5])
                                                           .arg(mainwindow->Num[6])
                                                           .toUtf8();
        serialport->write(data);
    }
}

void Total::tcpsendData()
{
    if(tcpsocket->state() == QAbstractSocket::ConnectedState)
    {
        QByteArray data = QString("%1 %2 %3 %4 %5 %6 ").arg(mainwindow->Num[1])
                                                       .arg(mainwindow->Num[2])
                                                       .arg(mainwindow->Num[3])
                                                       .arg(mainwindow->Num[4])
                                                       .arg(mainwindow->Num[5])
                                                       .arg(mainwindow->Num[6])
                                                       .toUtf8();
        QByteArray price = QString("%1").arg(countPrice(mainwindow->Num,mainwindow->Price)).toUtf8();

        tcpsocket->write("各商品数量：" + data + ",总价为：" + price);
    }
}


void Total::pushButton_4_clicked()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    tcpsocket->abort();
    tcpsocket->connectToHost(QHostAddress("192.168.10.100"),10001);
}

void Total::pushButton_3_clicked()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    tcpsocket->disconnectFromHost();
}
